<?php
namespace app\controller;

use app\BaseController;
use app\model\Note;

class NoteController extends BaseController
{
    public function getNoteAll()
    {
        $param = $this->request->param();
        try {
            $page=$param['page'];
        } catch (\Throwable $th) {
            $page=['pageSize'=>10,'pageNo'=>0];
        }
        $db = new Note();
        $data =$db->pageFindAll($page);
        $json=array('data'=>$data,'code'=>0,'tips'=>'ok');
        return json($json, 200);
    }

    public function insert()
    {
        $param = $this->request->param();
        $param['createTime']=time();
        $param['updateTime']=time();
        $data=$param;
        $db = new Note();
        if ($db->add($data)) {
            $json=array('data'=>[],'code'=>0,'tips'=>'ok');
        } else {
            $json=array('data'=>[],'code'=>2001,'tips'=>'新增文章失败');
        }
        return json($json, 200);
    }
    public function findById($id)
    {
        $db = new Note();
        $data = $db->findById($id);
        if (!$data->isEmpty()) {
            $json=array('data'=>$data,'code'=>0,'tips'=>'ok');
        } else {
            $json=array('data'=>[],'code'=>2002,'tips'=>'没有查询到数据');
        }
        return json($json, 200);
    }
    public function update()
    {
        $param = $this->request->param();
        $db = new Note();
        if (isset($param['noteId'])) {
            $param['updateTime']=time();
            if ($db->updateNote($param)) {
                $json=array('data'=>[],'code'=>0,'tips'=>'ok');
            } else {
                $json=array('data'=>[],'code'=>2002,'tips'=>'更新失败');
            }
        } else {
            $param['createTime']=time();
            $param['updateTime']=time();
            if ($db->add($param)) {
                $json=array('data'=>[],'code'=>0,'tips'=>'ok');
            } else {
                $json=array('data'=>[],'code'=>2001,'tips'=>'新增文章失败');
            }
        }
        return json($json, 200);
    }
    public function deleteData($id)
    {
        $db = new Note();
        $db->deleteNote($id);
        return json(['data'=>[],'code'=>0,'tips'=>'ok'], 200);
    }

    
}
